package org.dromara.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.business.domain.bo.GoodsCouponBo;
import org.dromara.business.domain.vo.GoodsCouponVo;
import org.dromara.business.service.GoodsCouponService;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 优惠券
 *
 * @author zlj
 * @date 2025-06-18
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/goodsCoupon")
public class GoodsCouponController extends BaseController {

    private final GoodsCouponService goodsCouponService;

    /**
     * 查询优惠券列表
     */
    @SaCheckPermission("business:goodsCoupon:list")
    @GetMapping("/list")
    public TableDataInfo<GoodsCouponVo> list(GoodsCouponBo bo, PageQuery pageQuery) {
        return goodsCouponService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出优惠券列表
     */
    @SaCheckPermission("business:goodsCoupon:export")
    @Log(title = "优惠券", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(GoodsCouponBo bo, HttpServletResponse response) {
        List<GoodsCouponVo> list = goodsCouponService.queryList(bo);
        ExcelUtil.exportExcel(list, "优惠券", GoodsCouponVo.class, response);
    }

    /**
     * 获取优惠券详细信息
     *
     * @param couponId 主键
     */
    @SaCheckPermission("business:goodsCoupon:query")
    @GetMapping("/{couponId}")
    public R<GoodsCouponVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long couponId) {
        return R.ok(goodsCouponService.queryById(couponId));
    }

    /**
     * 新增优惠券
     */
    @SaCheckPermission("business:goodsCoupon:add")
    @Log(title = "优惠券", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody GoodsCouponBo bo) {
        return toAjax(goodsCouponService.insertByBo(bo));
    }

    /**
     * 修改优惠券
     */
    @SaCheckPermission("business:goodsCoupon:edit")
    @Log(title = "优惠券", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody GoodsCouponBo bo) {
        return toAjax(goodsCouponService.updateByBo(bo));
    }

    /**
     * 删除优惠券
     *
     * @param couponIds 主键串
     */
    @SaCheckPermission("business:goodsCoupon:remove")
    @Log(title = "优惠券", businessType = BusinessType.DELETE)
    @DeleteMapping("/{couponIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] couponIds) {
        return toAjax(goodsCouponService.deleteWithValidByIds(List.of(couponIds), true));
    }

    /**
     * 查询优惠券
     */
    @GetMapping("/getCoupon")
    public R<List<GoodsCouponVo>> getCoupon() {
        return R.ok(goodsCouponService.getCoupon());
    }
}
